package com.ruoyi.rulengine;


import com.ruoyi.Application;
import com.ruoyi.iotcommon.domain.DeviceDataMessage;
import com.ruoyi.rulengine.domain.Rule;
import com.ruoyi.rulengine.domain.RuleAction;
import com.ruoyi.rulengine.domain.RuleCondition;
import com.ruoyi.rulengine.domain.emun.ActionType;
import com.ruoyi.rulengine.service.IRuleConditionService;
import com.ruoyi.rulengine.service.IRuleEngineService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootTest(classes = Application.class)
public class TestRulegine {

    private String productKey = "kdfkhjkl983y";
    private String channelKey = "property";
    private String deviceKey = "hjk54jgu9sn40";

    @Resource
    private IRuleEngineService ruleEngineService;

    @Resource
    private IRuleConditionService ruleConditionService;


    // 构造设备上报数据
    private DeviceDataMessage createDeviceData() {
        DeviceDataMessage deviceData = new DeviceDataMessage();
        deviceData.setProductKey(productKey);
        deviceData.setChannelKey(channelKey);
        deviceData.setDeviceKey(deviceKey);
        Map<String, Object> params = new HashMap<>();
        params.put("temperature", 50);
        params.put("humidity", 30);
        deviceData.setParams(params);
        return deviceData;
    }

    // 构造规则列表
    private List<Rule> createRules() {
        List<Rule> rules = new ArrayList<>();
        Rule rule = new Rule();
        rule.setLogical("AND");
        rule.setConditions(List.of(
                new RuleCondition(null, null,"temperature", ">", "30","温度",false),
                new RuleCondition(null, null, "humidity", "<", "50","湿度",false)
        ));
        rule.setActions(List.of(
                new RuleAction(null, null, ActionType.DEVICE_CMD.getName(), ActionType.DEVICE_CMD.getCode(), "{\"cmd\":\"turnOn\"}",null,null),
                new RuleAction(null, null, ActionType.URL_CALL.getName(), ActionType.URL_CALL.getCode(), "{\"url\":\"http:12345.com\"}",null,null)
        ));
        rules.add(rule);
        return rules;
    }

    @Test
    public void testPerformance() {

            long startTime = System.currentTimeMillis();
            DeviceDataMessage deviceData = createDeviceData();
            List<Rule> rules = createRules();
            // 异步执行规则引擎
            ruleEngineService.evaluateRules(rules, deviceData);

            long endTime = System.currentTimeMillis();
            long executionTime = endTime - startTime;

            // 打印每次执行时间
            System.out.println("Execution time for iteration  " + executionTime + " ms");

    }

    @Test
    public void queryInsert(){
        List<RuleCondition> ruleConditions = ruleConditionService.selectRuleConditionList(new RuleCondition());
        log.info("ruleConditions size {}", ruleConditions.size());
    }

}
